package com.mengxuegu.member.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.mengxuegu.member.base.Result;
import com.mengxuegu.member.entity.Staff;
import com.mengxuegu.member.req.StaffREQ;
import com.mengxuegu.member.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author 梦学谷-www.mengxuegu.com
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private IStaffService staffService;

    /**
     * 分页条件查询 localhost:6666/staff/list/search/1/20
     * @param page
     * @param size
     * @param req
     * @return
     */
    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody(required = false) StaffREQ req) {
        return staffService.search(page, size, req);
    }

    /**
     * 新增  localhost:6666/staff  POST
     * @param staff
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Staff staff) {
        return staffService.add(staff);
    }

    /**
     * 删除员工 DELETE  localhost:6666/staff/7
     * @param id 员工id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id) {
        boolean b = staffService.removeById(id);
        if(b) {
            return Result.ok();
        }

        return Result.error("删除员工信息失败");
    }

    /**
     * 查询员工详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable int id) {
        Staff staff = staffService.getById(id);
        return Result.ok(staff);
    }

    /**
     * 修改员工信息
     * @param id
     * @param staff
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable int id,
                         @RequestBody Staff staff) {
        return staffService.update(id, staff);
    }

}
